import React from 'react';
import { obj_int, is_empty, Info, loc, ck_int, useKForm, KDlg, KInput } from 'sui';

const DlgNum = (props) => {
    const form = useKForm(props.data);

    const ck = {
        dnum: v => {
            const e = ck_int(v);
            if (e) {
                return e;
            }

            if (props.fin === "check") {
                const old = obj_int(props.data, "dnum");
                const now_v = parseInt(v, 10);

                if (now_v <= old) {
                    return loc("l_155");
                }
            }

            return null;
        }
    };

    const getCol = () => {
        const col = props.col;
        if (is_empty(col)) {
            return null;
        }

        const v = ["sn", "tech", "dnum"];
        const set = new Set();

        v.forEach(d => set.add(d));

        const tmp = [];

        col.forEach(d => {
            if (set.has(d.sn)) {
                tmp.push(d);
            }
        });

        return tmp;
    };

    const getTitle = () => {
        if (props.fin === "check") {
            return loc("l_154");
        }

        return loc("l_740");
    };

    return (
        <KDlg {...props} title={getTitle()} ck={ck} form={form}>
            <Info data={props.data} col={getCol()} hl={["dnum"]}/>
            <KInput label={loc("l_127")} form={form} name="dnum"/>
        </KDlg>
    );
};

export {DlgNum};
